package com.gitee.wsl.collections.iterator


class ArrayIterator<E>(
    values: Array<E>,
    start: Int = 0,
    end: Int = values.lastIndex
) : Iterator<E> {

    val values: Array<E>
    val end: Int
    var currentIndex: Int

    init {
        require(start <= end && end <= values.size)

        this.values = values
        this.end = end
        this.currentIndex = start
    }

    public override fun hasNext(): Boolean {
        return currentIndex < end
    }

     override fun next(): E {
        if (!hasNext()) {
            throw NoSuchElementException()
        }
        return values[currentIndex++]
    }

//    public override fun remove() {
//        throw UnsupportedOperationException()
//    }

    companion object {

        fun <E> of(vararg array: E): Iterator<E> {
            return ArrayIterator(array, 0, array.lastIndex)
        }

        fun <E> of(array: Array<E>, start: Int, length: Int): Iterator<E> {
            return ArrayIterator(array, start, start + length)
        }
    }
}
